//
//  PEDeviceInverterStatisticsCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/6/13.
//

#import "RHInverterStatisticsCell2.h"
#import "DeviceChartFieldModel.h"

@interface RHInverterStatisticsCell2()
@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) UIImageView *icon;

@property (nonatomic, strong) NSMutableArray *itemsArray;

@property (nonatomic, strong) UIView *itemBgView;
@end
@implementation RHInverterStatisticsCell2

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setModel:(RHInverterModel *)model
{
    _model = model;
    self.titleLab.text = model.title.rh_localized;
    self.icon.image = [UIImage imageNamed:model.icon];
    [model.itemArr enumerateObjectsUsingBlock:^(DeviceChartFieldModel * _Nonnull fieldModel, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx<self.itemsArray.count) {
            UIView *itemView = self.itemsArray[idx];
            QMUILabel *titleLab = [itemView viewWithTag:100+idx];
            QMUILabel *contentLab = [itemView viewWithTag:200+idx];
            titleLab.text = fieldModel.keyName;
            NSString *valueStr = [NSDecimalNumber decimalStringWithDoubleString:fieldModel.content nullStr:@"0"];
            NSString *content = [NSString stringWithFormat:@"%@ (%@)",ValidNullDataToString(valueStr, @"--"),ValidNullDataToString(fieldModel.unitName, @"--")];
            NSString *key = [NSString stringWithFormat:@"(%@)",fieldModel.unitName];
            contentLab.attributedText = [NSString attributeStringWithContent:content attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX_A(@"#3F4443", 0.6) attriKey:key];
            
            //英文状态下超出显示范围
            CGFloat limitWidth = floor((kUI_WINDOW_W - 30)/2) - 25-6;
            NSLog(@"----->limitWidth = %f",limitWidth);
            CGFloat height = [titleLab.text getStringHeightFont:titleLab.font limitWidth:limitWidth];
            NSLog(@"---->titleName = %@  height = %f",titleLab.text,height);
            if (height > 20) {
                [titleLab mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(3);
                    make.right.mas_offset(-3);
                    make.top.equalTo(itemView.mas_centerY).offset(3);
                    make.bottom.mas_offset(0);
                }];
            }else{
                [titleLab mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(3);
                    make.right.mas_offset(-3);
                    make.top.equalTo(itemView.mas_centerY).offset(3);
                }];
            }
        }
    }];
    
    //重新布局
    [self.itemBgView qmui_removeAllSubviews];
    __block UIView *lastview;
    __block CGFloat itemWidth = floor((kUI_WINDOW_W - 30)/2 - 25);
    NSLog(@"----->itemWidth = %f",itemWidth);
    kWeakSelf;
    [model.itemArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView  = [weakSelf.itemsArray safeObjectAtIndex:idx];
        if (itemView == nil) {
            return;
        }
        UIView *line = [itemView viewWithTag:500];
        if (model.itemArr.count == 1) {//一个的时候不要竖线
            line.hidden = YES;
        }else{
            line.hidden = NO;
        }
        [weakSelf.itemBgView addSubview:itemView];
        if (idx == 0) {
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(20);
                make.top.mas_offset(0);
                make.width.offset(itemWidth);
                make.height.offset(65);
            }];
        }else{
            
            if (idx % 2) {
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.size.equalTo(lastview);
                    make.left.equalTo(weakSelf.itemBgView.mas_centerX).offset(20);
                    make.top.equalTo(lastview);
                }];
            }else{
                //换行
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(20);
                    make.top.equalTo(lastview.mas_bottom);
                    make.size.equalTo(lastview);
                }];
                //横线
                UIView *line = kView;
                line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
                [weakSelf.itemBgView addSubview:line];
                [line mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(1);
                    make.left.mas_offset(20);
                    make.right.mas_offset(-20);
                    make.top.equalTo(lastview.mas_bottom);
                }];
            }
            
        }
        
        lastview = itemView;
    }];
    //封底
    [lastview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
}


- (void)setupUI{
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    [bgView rounded:8 width:1 color:kBorderColor];
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    
    UIImageView *iconImg = [[UIImageView alloc]init];
    [bgView addSubview:iconImg];
    self.icon = iconImg;
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.top.mas_offset(15);
    }];
    
    QMUILabel *titleLab = [QMUILabel labelWithText:@"--" font:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#3F4443")];
    [bgView addSubview:titleLab];
    self.titleLab = titleLab;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImg.mas_right).offset(10);
        make.centerY.equalTo(iconImg);
    }];
    
    UIView *itemBgView = kView;
    [bgView addSubview:itemBgView];
    self.itemBgView = itemBgView;
    [itemBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(iconImg.mas_bottom).offset(5);
        make.bottom.mas_offset(-5);
    }];
    
    self.itemsArray = [NSMutableArray array];
    CGFloat bgWidth = MIN(kUI_WINDOW_W, kUI_WINDOW_H) - 30;
    __block UIView *lastview;
    NSArray *titles = @[@"--",@"--",@"--",@"--",@"--",@"--"];
    __block CGFloat itemWidth = (kUI_WINDOW_W - 30)/2 - 25;
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView  = [self subItimeViewWithTitle:obj index:idx];
//        itemView.backgroundColor = kRandomColor;
        [itemBgView addSubview:itemView];
        [self.itemsArray addObject:itemView];
        if (idx == 0) {
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(20);
                make.top.mas_offset(0);
                make.width.offset(itemWidth);
                make.height.offset(65);
            }];
        }else{
            
            if (idx % 2) {
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.size.equalTo(lastview);
                    make.left.equalTo(itemBgView.mas_centerX).offset(20);
                    make.top.equalTo(lastview);
                }];
            }else{
                //换行
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(20);
                    make.top.equalTo(lastview.mas_bottom).offset(1);
                    make.size.equalTo(lastview);
                }];
                //横线
                UIView *line = kView;
                line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
                [itemBgView addSubview:line];
                [line mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(1);
                    make.left.mas_offset(20);
                    make.right.mas_offset(-20);
                    make.top.equalTo(lastview.mas_bottom);
                }];
            }
            
        }
        //竖线
        if ( idx%2 == 0) {
            UIView *line = kView;
            line.tag = 500;
            line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
            [itemView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(1);
                make.height.offset(35);
                make.centerY.mas_offset(0);
                make.right.mas_offset(0);
            }];
        }
        lastview = itemView;
    }];
    //封底
    [lastview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
}






-(UIView*)subItimeViewWithTitle:(NSString *)title index:(NSInteger)index
{
    UIView *itemView = kView;
    QMUILabel *subTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#3F4443",0.6) textAligment:NSTextAlignmentLeft];
    subTitleLab.text = title.rh_localized;
    subTitleLab.tag = 100+index;
    subTitleLab.numberOfLines = 2;
//    subTitleLab.adjustsFontSizeToFitWidth = YES;
//    subTitleLab.minimumScaleFactor = 0.8;
    [itemView addSubview:subTitleLab];
    
    QMUILabel *contentLab = [QMUILabel labelWithFont:kSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#3F4443") textAligment:NSTextAlignmentLeft];
    contentLab.tag = 200+index;
    [itemView addSubview:contentLab];
    
    [subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(3);
        make.right.mas_offset(-3);
        make.top.equalTo(itemView.mas_centerY).offset(3);
        
    }];
    
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(3);
        make.right.mas_offset(-3);
        make.bottom.equalTo(itemView.mas_centerY).offset(-3);
    }];
    return itemView;
}

@end
